package com.inspirationandgenius.welcome;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.lifeofdata.inspirationandgenius.R;
import com.lifeofdata.inspirationandgenius.R.layout;
import com.lifeofdata.inspirationandgenius.R.menu;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class Login extends Activity implements OnClickListener {

	ImageView imagev;
	EditText pwdtext;
	TextView logintext;
	Button loginbtn;
	DBAdapter db;
	String lk,pwdd,igcnuserid;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        db = new DBAdapter(this);
        db.open();
        if(this.getIntent().getExtras()!=null)
        {
        	Bitmap im = (Bitmap) this.getIntent().getParcelableExtra("image");
        	imagev = (ImageView)findViewById(R.id.textvuser);
        	imagev.setImageBitmap(im);
        	logintext = (TextView)findViewById(R.id.textViewlogin);
        	lk=this.getIntent().getExtras().getString("login");
        	igcnuserid=this.getIntent().getExtras().getString("id");
        	logintext.setText(lk);
        	loginbtn = (Button)findViewById(R.id.Connect);
        	loginbtn.setOnClickListener(this);
        	pwdtext = (EditText)findViewById(R.id.usrmdp);
        	
        	
        }
        
    }

    
	public void onClick(View v) {
		
		 MessageDigest md = null;
			try {
				md = MessageDigest.getInstance("MD5");
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		        md.update((pwdtext.getText().toString()).getBytes());
		 
		        byte byteData[] = md.digest();
		 
		        //convert the byte to hex format method 1
		        StringBuffer sb = new StringBuffer();
		        for (int i1 = 0; i1 < byteData.length; i1++) {
		         sb.append(Integer.toString((byteData[i1] & 0xff) + 0x100, 16).substring(1));
		        }
		 
		        System.out.println("Digest(in hex format):: " + sb.toString());
		 
		        //convert the byte to hex format method 2
		        StringBuffer hexString = new StringBuffer();
		    	for (int i1=0;i1<byteData.length;i1++) {
		    		String hex=Integer.toHexString(0xff & byteData[i1]);
		   	     	if(hex.length()==1) hexString.append('0');
		   	     	hexString.append(hex);
		    	}
		    	System.out.println("Digest(in hex format):: " + hexString.toString());
			 
			if(v.getId() == R.id.Connect)
			{
				Cursor c =db.getigUsers(lk, hexString.toString());
				
				if(c.getCount()>0)
				{
					db.update_igUser(lk, hexString.toString());
					Intent t = new Intent(getApplicationContext(), Tower.class);
					t.putExtra("enter", "yes");
					t.putExtra("loginn", lk);
					t.putExtra("pwdd", hexString.toString());
					t.putExtra("igcnuserid", igcnuserid);
					startActivityForResult(t, 1234);
					
					finish();
				}
				else
				{
					Intent t = new Intent(getApplicationContext(), ControlTower.class);
					t.putExtra("enter", "no");
					t.putExtra("login", lk);
					t.putExtra("pwd", hexString.toString());
					startActivityForResult(t, 1234);
					finish();
				}
			}
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		db.close();
	}
}
